package com.SSH_Test.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Component;

import com.SSH_Test.pojo.TestUser;


@Component("testdao")
public class TestDao {
	private DataSource dataSource;

	public DataSource getDataSource() {
		return dataSource;
	}
	@Resource(name = "myDataSource")
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	public List<TestUser> getTestUser() {
		Connection conn=null;
		  Statement stmt = null;
		  ResultSet rs = null;
		  List<TestUser> list=new ArrayList<TestUser>();
		try {
			conn=dataSource.getConnection();
			stmt =conn.createStatement();
			rs=stmt.executeQuery("select * from t_users");
			   while(rs.next()){
				   TestUser tu = new TestUser();
				    tu.setId(rs.getInt("id"));
				    tu.setName(rs.getString("name"));
				    tu.setAge(rs.getInt("age"));
				    list.add(tu);
				   }
			   conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			
		}
		for(TestUser u:list){
			System.out.println(u.getId()+u.getName());
		}
		return list;
	}
	
}
